package example;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

/**
写一个案例，利用IP流，完成文件的复制操作，自由发挥
 */
public class CopyFile {
    public static void main(String[] args) {
        try {
            File file = new File("test.obj");
            file.createNewFile();
            copy(file);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static File copy(File file) throws IOException {
        String path = file.getName();
        String []str=path.split("\\.");

        File file_cp = new File(str[0]+"_cp."+str[1]);
        FileInputStream fis =  new FileInputStream(file);
        byte[]data = new byte[1024*10];
        FileOutputStream fos = new FileOutputStream(file_cp);
        while(fis.read(data)!=-1){
            fos.write(data);
        }
        return file_cp;
    }
}
